/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Enterprise;

/**
 *
 * @author Adriano
 */
public class EnterpriseDAO implements DAO {

    private final DataSource dataSource;

    public EnterpriseDAO() {
        dataSource = new DataSource();
    }

    public void create(Object o) {
        try {
            String sql = "INSERT INTO enterprise VALUES (null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            Enterprise empresa = (Enterprise) o;

            ps.setString(1, empresa.getTxtCorporateName());
            ps.setString(2, empresa.getTxtCnpj());
            ps.setString(3, empresa.getTxtStateRegistration());
            ps.setString(4, empresa.getTxtMunicipalRegistration());
            ps.setString(5, empresa.getTxtZipcode());
            ps.setString(6, empresa.getTxtAddress());
            ps.setString(7, empresa.getTxtNumero());
            ps.setString(8, empresa.getTxtComplement());
            ps.setString(9, empresa.getTxtDistrict());
            ps.setString(10, empresa.getTxtCity());
            ps.setString(11, empresa.getTxtLstUF());
            ps.setString(12, empresa.getTxtDdd());
            ps.setString(13, empresa.getTxtPhone());
            ps.setString(14, empresa.getTxtEmail());
            ps.setString(15, empresa.getTxtJointStock());
            ps.setString(16, empresa.getTxtLstStatus());

            int res = ps.executeUpdate();
            if (res == 0) {
                System.err.println("EMPRESADAO.CREATE - Nenhum empresa criado !");
            }
        } catch (SQLException e) {
            System.err.println("EMPRESADAO.CREATE  - " + e.getMessage());
        }
    }

    public Object read(Object o) {

        try {

            String sql = "SELECT * FROM enterprise";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);
            Enterprise query = (Enterprise) o;

            ps.setString(1, query.getTxtCorporateName());
            ps.setString(2, query.getTxtCnpj());
            ps.setString(3, query.getTxtStateRegistration());
            ps.setString(4, query.getTxtMunicipalRegistration());
            ps.setString(5, query.getTxtZipcode());
            ps.setString(6, query.getTxtAddress());
            ps.setString(7, query.getTxtNumero());
            ps.setString(8, query.getTxtComplement());
            ps.setString(9, query.getTxtDistrict());
            ps.setString(10, query.getTxtCity());
            ps.setString(11, query.getTxtLstUF());
            ps.setString(12, query.getTxtDdd());
            ps.setString(13, query.getTxtPhone());
            ps.setString(14, query.getTxtJointStock());
            ps.setString(15, query.getTxtEmail());
            ps.setString(16, query.getTxtLstStatus());

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {

                Enterprise empresa = new Enterprise();

                empresa.setTxtId(rs.getString("id"));
                empresa.setTxtCorporateName("CorporateName");
                empresa.setTxtCnpj("CNPJ");
                empresa.setTxtStateRegistration("StateRegistration");
                empresa.setTxtMunicipalRegistration("MunicipalRegistration");
                empresa.setTxtZipcode("ZipCode");
                empresa.setTxtAddress("Address");
                empresa.setTxtNumero("Numero");
                empresa.setTxtComplement("Complement");
                empresa.setTxtDistrict("District");
                empresa.setTxtCity("City");
                empresa.setTxtLstUF("UF");
                empresa.setTxtDdd("DDD");
                empresa.setTxtPhone("Phone");
                empresa.setTxtJointStock("CapitalSocial");
                empresa.setTxtEmail("Email");
                empresa.setTxtLstStatus("Status");
                empresa.setTxtJointStock("CapitalSocial");
                empresa.setTxtLstStatus("Status");

                return empresa;
            }
        } catch (SQLException e) {
            System.err.println("USERDAO.READ - " + e.getMessage());
        }
        return null;
    }

    public void delete(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void update(Object o) {
        try {

            String sql = "UPDATE enterprise SET CorporateName=?,Cnpj =?,StateRegistration =?,MunicipalRegistration=?,\n"
                    + "ZipCode =?,Address = ?,Numero = ?,Complement = ?,District = ?,City = ?,UF = ?,DDD = ?,\n"
                    + "Phone = ?,Email = ?,CapitalSocial = ?,Status = ?\n"
                    + "WHERE id = ?;";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            Enterprise query = (Enterprise) o;

            ps.setString(1, query.getTxtCorporateName());
            ps.setString(2, query.getTxtCnpj());
            ps.setString(3, query.getTxtStateRegistration());
            ps.setString(4, query.getTxtMunicipalRegistration());
            ps.setString(5, query.getTxtZipcode());
            ps.setString(6, query.getTxtAddress());
            ps.setString(7, query.getTxtNumero());
            ps.setString(8, query.getTxtComplement());
            ps.setString(9, query.getTxtDistrict());
            ps.setString(10, query.getTxtCity());
            ps.setString(11, query.getTxtLstUF());
            ps.setString(12, query.getTxtDdd());
            ps.setString(13, query.getTxtPhone());
            ps.setString(14, query.getTxtEmail());
            ps.setString(15, query.getTxtJointStock());
            ps.setString(16, query.getTxtLstStatus());
            ps.setString(17, query.getTxtId());

            ps.executeUpdate();

        } catch (SQLException e) {
            System.err.println("UPDATEUSER - " + e.getMessage());
        }

    }

    public Object findAll() {
        try {

            String sql = "SELECT * FROM enterprise";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            ArrayList<Enterprise> listaEmpresa = new ArrayList<Enterprise>();

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {

                Enterprise empresa = new Enterprise();

                empresa.setTxtId(rs.getString("id"));
                empresa.setTxtCorporateName(rs.getString("CorporateName"));
                empresa.setTxtCnpj(rs.getString("cnpj"));
                empresa.setTxtStateRegistration(rs.getString("stateRegistration"));
                empresa.setTxtMunicipalRegistration(rs.getString("municipalRegistration"));
                empresa.setTxtZipcode(rs.getString("zipcode"));
                empresa.setTxtAddress(rs.getString("address"));
                empresa.setTxtNumero(rs.getString("numero"));
                empresa.setTxtComplement(rs.getString("complement"));
                empresa.setTxtDistrict(rs.getString("district"));
                empresa.setTxtCity(rs.getString("city"));
                empresa.setTxtLstUF(rs.getString("uf"));
                empresa.setTxtJointStock(rs.getString("CapitalSocial"));
                empresa.setTxtDdd(rs.getString("ddd"));
                empresa.setTxtPhone(rs.getString("phone"));
                empresa.setTxtEmail(rs.getString("email"));
                empresa.setTxtLstStatus(rs.getString("status"));

                listaEmpresa.add(empresa);

            }
            return listaEmpresa;

        } catch (SQLException e) {
            System.err.println("EMPRESAFINDALL.READ - " + e.getMessage());
            return null;
        }

    }

    public Object find(Object o) {

        try {

            String sql = "SELECT * FROM enterprise WHERE Cnpj = ?";
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);

            Enterprise query = (Enterprise) o;
            ps.setString(1, query.getTxtCnpj());
          

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                Enterprise e = new Enterprise();
                e.setTxtId(rs.getString("Id"));
                e.setTxtCorporateName(rs.getString("CorporateName"));
                e.setTxtCnpj(rs.getString("Cnpj"));
                e.setTxtStateRegistration(rs.getString("StateRegistration"));
                e.setTxtMunicipalRegistration(rs.getString("MunicipalRegistration"));
                e.setTxtZipcode(rs.getString("ZipCode"));
                e.setTxtAddress(rs.getString("Address"));
                e.setTxtNumero(rs.getString("Numero"));
                e.setTxtComplement(rs.getString("Complement"));
                e.setTxtDistrict(rs.getString("District"));
                e.setTxtCity(rs.getString("City"));
                e.setTxtLstUF(rs.getString("UF"));
                e.setTxtDdd(rs.getString("DDD"));
                e.setTxtPhone(rs.getString("Phone"));
                e.setTxtEmail(rs.getString("Email"));
                e.setTxtJointStock(rs.getString("CapitalSocial"));
                e.setTxtLstStatus(rs.getString("Status"));

                System.out.println("DEBUG " + ps.toString());
                return e;
            }
        } catch (SQLException e) {
            System.err.println("BUSCAENTERPRISEDAO - " + e.getMessage());
        }
        return null;

    }

    public void closeDAO() {
        dataSource.closeConnection();
    }
}
